<?php

/**
 * ControllerBase
 *
 * @author HEO
 */
class ControllerBase extends CController {

    public $layout = '//layouts/main';
    public $treeview = null;
    public $menu = array();
    public $breadcrumbs = array();
    public $title = SITE_NAME;
    public $porletname = "";
    public $mainTitle = '';

//    protected $message;

    public function init() {
        Yii::app()->language = (isset(Yii::app()->request->cookies['language']->value)) ? Yii::app()->request->cookies['language']->value : 'en';
//        $config = unserialize(VariableBase::model()->findByPk('config')->value);
//        if(is_array($config))
//            Yii::app()->params = $config;
        
        Yii::app()->user->setState('pageSize', (int) Yii::app()->params['limit_page']);
    }

    public function setMessage($value) {
        Yii::app()->session['Message'] = $value;
    }

    public function getMessage() {
        $message = Yii::app()->session['Message'];
        unset(Yii::app()->session['Message']);
        return $message;
    }

    public function hasFlash() {
        return (isset(Yii::app()->session['Message']));
    }

    public function renderFlash() {
        if ($this->hasFlash()) {
            echo '<div class="flash-notice">';
            echo $this->getMessage();
            echo '</div>';
            Yii::app()->clientScript->registerScript('fade', "setTimeout(function() { $('.flash-notice').fadeOut('slow'); }, 5000);");
        }
    }

    /**
     * Redirects and forwards message to new destination
     * @param string $url
     * @param boolean $terminate 
     */
    public function redirect($url, $terminate = true) {
        $errorHtml = Yii::app()->getErrorHandler()->getError();
        if ($errorHtml != null) {
            Yii::app()->session['ErrorMessage'] = $errorHtml;
        }
        parent::redirect($url, $terminate);
    }
}
